<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Logic;


use App\Common\Logger;
use App\Exceptions\ApiException;
use App\Http\Controllers\Base;
use Illuminate\Support\Facades\DB;

class VipLogic extends Logic
{
    /**
     * 每日VIP首次登陆|首次开通 的后置操作
     * @param $goods_id
     * @param $uid
     * @param $filed
     * @throws \App\Exceptions\ApiException
     */
    public function vipConfPostPosition($goods_id, $uid, $filed)
    {
        $rule = DB::table('t_item_activity_conf')->where('type',function ($query) use ($goods_id,$filed){
            $query->from('t_vip_conf')->where('id',$goods_id)->select([$filed]);
        })->where('status',1)->value('rule');

        if (!empty($rule)) {
            if ($filed == 'activity_conf_type_login') {
                $user_object = DB::table('t_player_info')->where('id', $uid)->first(['diamonds', 'gold', 'room_ticket']);
                (int)$last_count = DB::table('t_quickly_grab_ticket_info')->where('uid', $uid)->value('count');
                $head = '会员登陆奖励';
                $content = '昨日:' . "钻石:{$user_object->diamonds}、房卡:{$user_object->room_ticket}、快抢赛次数:{$last_count}\n";
            } else {
                $head = '首次开通会员奖励';
                $content = '首次开通会员奖励';
            }
            DB::table('t_mail_info')->insert([
                'uid' => $uid,
                'type' => 2,
                'head' => $head,
                'content' =>  $content ,
                'rule' => $rule,
                'status' => 1
            ]);
        }
    }

    //获取VIP等级
    public function vipInfo($uid)
    {
        $vip_info_object = DB::table('t_vip_info')->where('uid',$uid)->where('expire_time','>',date('Y-m-d H:i:s'))->first(['vip_conf_id','expire_time']);
        if (empty($vip_info_object)) {
            return ['name' => '普通会员', 'level' => 0,'expire_time' => ''];
        }
        $vip_conf_object = DB::table('t_vip_conf')->where('id',$vip_info_object->vip_conf_id)->first(['name','level']);
        return ['name' => $vip_conf_object->name, 'level' => $vip_conf_object->level,'expire_time' => $vip_info_object->expire_time];
    }

    /**
     * 处理支付成功后的操作
     * @throws \App\Exceptions\ApiException
     */
    public function handlePayCallBack($goods_id,$uid,$out_trade_no,$xml)
    {
        $vip_conf = DB::table('t_vip_conf')->where('id', $goods_id)->first(['expire_time', 'give_number']);
        if (empty($vip_conf)) {
            throw new ApiException('VIP商品为空');
        }
        $vip_info = DB::table('t_vip_info')->where('uid', $uid)->first(['expire_time', 'vip_conf_id']);

        //如果有数据,代表今日已经免费领取过
        //如果领取过,升级会员的时候把当前会员免费的次数 - 上次会员免费的次数
        $vip = DB::table('t_quickly_grab_ticket_info')->where('uid', $uid)->where('date', date('Y-m-d'))->value('vip_level');
        if (!is_null($vip)) {
            //免费会员
            if ($vip == 0) {
                $give_number               = 1;
                $quickly_grab_ticket_count = $vip_conf->give_number - $give_number;
            } else {
                $give_number               = DB::table('t_vip_conf')->where('id', $vip_info->vip_conf_id)->value('give_number');
                $quickly_grab_ticket_count = $vip_conf->give_number - $give_number;
            }
            DB::table('t_quickly_grab_ticket_info')->where('uid', $uid)->update(['count' => $quickly_grab_ticket_count]);
        }

        if (empty($vip_info)) {
            DB::table('t_vip_info')->insertGetId([
                'uid' => $uid,
                'vip_conf_id' => $goods_id,
                'expire_time' => date('Y-m-d H:i:s', strtotime("+ $vip_conf->expire_time month"))
            ]);
            //开通会员成功立即送开通福利
            $this->vipConfPostPosition($goods_id, $uid, 'activity_conf_type_open');
            //也把每日登陆的福利送过去,否侧当天已经不是首次登陆,导致开通首日的登陆礼包无法领取
            $this->vipConfPostPosition($goods_id, $uid, 'activity_conf_type_login');
        } else {
            if (!DB::table('t_vip_info')
                ->where('uid', $uid)
                ->update(['expire_time' => date('Y-m-d H:i:s', strtotime("+ $vip_conf->expire_time month", $vip_info->expire_time))])) {
                Logger::instance()->wxPayCallback("【VIP：更新用户权益的过期时间失败】:\n$xml\n");
                PayCallBackLogic::instance()->updatePay($out_trade_no,'开通会员回调处理失败');
            }
        }
    }


    public function conversionDay(string $date)
    {
        $expire = strtotime($date);
        $surplus_time = $expire - time();
        if ($surplus_time < 1) {
            return 0;
        }
        return floor($surplus_time / 86400);
    }
}